﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;

namespace TireService.Web.View.CustomerView
{
    public partial class CarList : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            // Bind carstatus to dropdownlist
            TireServiceDB db = new TireServiceDB();
            ddlCarStatus.DataSource = db.GetCarStatus();
            ddlCarStatus.ValueField = "CarStatusId";
            ddlCarStatus.TextField = "Description";
            ddlCarStatus.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Bind car data to gridview
            BindCustomerCar(db);

            if (Page.IsPostBack != true)
            {
                GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
            }
            
            // Change gridview pagesize based on dropdownlist
            gvCar.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            // Dispose connection to database
            db.Dispose();
        }

        #region Private Functions

        /// <summary>
        /// Function that bind customer cars to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindCustomerCar(TireServiceDB db)
        {
            // Bind to gridview
            gvCar.DataSource = db.GetCars();
            gvCar.DataBind();
        }

        /// <summary>
        /// Function that change grouping in order gridview
        /// </summary>
        /// <param name="ViewIndex">Integer setting the grouping mode</param>
        protected void GroupOrderData(int ViewIndex)
        {
            gvCar.ClearSort();
            gvCar.GroupSummary.Clear();
            switch (ViewIndex)
            {
                case 0:
                    gvCar.GroupBy(gvCar.Columns["CarStatusDescription"]);

                    break;
                //case 1:
                //    gvCar.GroupBy(gvCar.Columns["CarStatusDescription"]);

                //    break;
                //case 2:
                //    gvOrder.GroupBy(gvOrder.Columns["CustomerNumber"]);

                //    break;
                //case 3:
                //    gvOrder.GroupBy(gvOrder.Columns["PhotoDate"]);
                //    gvOrder.GroupBy(gvOrder.Columns["CustomerNumber"]);

                //break;
            }
            gvCar.ExpandAll();
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }

        /// <summary>
        /// Dropdown list function that change gridview grouping when selected item is changed
        /// </summary>
        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Group data
            GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
        }

        /// <summary>
        /// Callback function that updates status on selected cars in gridview
        /// </summary>
        protected void cbSaveCarStatus_Callback(object sender, CallbackEventArgs e)
        {
            // Values from client - selected values in gridview
            string[] param = e.Parameter.Split(',');

            if (param.Length != 0)
            {
                TireServiceDB db = new TireServiceDB();

                // Get status value (last item in array)
                int CarStatus = Convert.ToInt32(param[param.Length - 1]);

                // Run through array (except last value, which is new car status
                for (int i = 0; i < param.Length-1; i++)
                {
                    // Update each car
                    Car car = db.GetCar(Convert.ToInt32(param[i]));
                    car.CarStatusId = CarStatus;
                }

                // Commit changes to database
                db.SubmitChanges();

                // Refresh gridview
                BindCustomerCar(db);

                // Dispose database connection
                db.Dispose();
            }
            else
            {
                throw new ArgumentException("Ingen biler valgt.");
            }
        }

        #endregion

        #region GridView Functions

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvCar_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            //// Format UnBoundColumn CustomerSubscriptionDescription: Show subscription description
            //if (e.Column.FieldName == "CustomerSubscriptionDescription")
            //{
            //    // Get subscriptionid
            //    int subscriptionid = Convert.ToInt32(e.GetListSourceFieldValue("SubscriptionId"));

            //    TireServiceDB db = new TireServiceDB();

            //    // Change field value
            //    e.Value = db.GetSubscriptionDescription(subscriptionid);

            //    db.Dispose();
            //}

            // Format UnBoundColumn BrandDescription: Show subscription description
            if (e.Column.FieldName == "BrandDescription")
            {
                // Get brandid
                int brandid = Convert.ToInt32(e.GetListSourceFieldValue("CarBrandId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                e.Value = db.GetCarBrandDescription(brandid);

                db.Dispose();
            }

            // Format UnBoundColumn CarStatusDescription: Show car status
            if (e.Column.FieldName == "CarStatusDescription")
            {
                // Get carstatusid
                int carstatusid = Convert.ToInt32(e.GetListSourceFieldValue("CarStatusId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                e.Value = db.GetCarStatusDescription(carstatusid);

                db.Dispose();
            }
        }

        #endregion
    }
}